package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.User;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录权限校验过滤器
 * 1.0 定义类，实现接口Filter
 * 2.0实现方法，doFilter
 * 3.0注册过滤器，在类上打上注解@WebFilter("/*)
 * 4.0在springBoot环境下，要想要过滤器生效，需要在启动类上加上注解@ServletCompoenScan
 */

@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {

    //路径匹配器，获取通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取本次的uri
        String requestURI = request.getRequestURI();
      /*  System.out.println("requestURI");
        System.out.println(requestURI);*/
        //定义不需要请求的路径
        String[] urls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/code",
                "/user/login"
        };
        //判断本次请求是否需要拦截
        boolean check = check(urls, requestURI);
        //如果不需要拦截直接放行
        if (check){
            filterChain.doFilter(request,response);
            return;
        }
        //判断是否已经登录，如登录直接放行
        if(request.getSession().getAttribute("employee")!=null){
            Employee employee = (Employee) request.getSession().getAttribute("employee");
            Long id = employee.getId();
            BaseContext.setCurrentId(id);
            filterChain.doFilter(request,response);
            return;
        }

        //判断用户是否已经登录，如登录直接放行
        if (request.getSession().getAttribute("user")!=null){
            User user = (User) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(user.getId());
            filterChain.doFilter(request,response);
            return;
        }
        response.setContentType("text/json;charset=utf-8");
          //用户未登录
       response.getWriter().write(JSON.toJSONString(R.error("未登录")));
        return;
    }

    public boolean check(String[] urls, String requestURI) {
        boolean flag=false;
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match){
               flag= true;
            }

        }
        return flag;
    }

    @Override
    public void destroy() {

    }
}
